<!DOCTYPE html>
<html>
<head>
<title>订单列表</title>
<meta charset="utf-8"/>
<link rel="stylesheet" href="../../css/bootstrap.css"/>
<script type="text/javascript" src="../../js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../../layer/layer.js"></script>
<script type="text/javascript" src="../../js/auth.js"></script>
<script type="text/javascript" src="../../js/CookieShop.js"></script>
</head>
<body>
<div class="container-fluid">
    <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="../index.html">蛋糕商城</a>
                <a class="navbar-brand" href="../../pages/index.html">集成后台</a>
            </div>
            <div>
                <ul class="nav navbar-nav">
                    <li ><a href="../../custpages/admin/order_list.html">订单管理</a></li>
                    <li ><a href="../../custpages/admin/user_list.html">客户管理</a></li>
                    <li ><a href="../../custpages/admin/goods_list.html">商品管理</a></li>
                    <li ><a href="../../custpages/admin/type_list.html">类目管理</a></li>
                    <li><a href="javascript:cookieShopLogout()">退出</a></li>
                </ul>
            </div>
        </div>
    </nav>
	<br>
	<ul role="tablist" class="nav nav-tabs">
		<li role="presentation"><a href="../admin/order_list.html">全部订单</a></li>
		<li role="presentation"><a href="../admin/order_list.html?statusId=1">未付款</a></li>
		<li role="presentation"><a href="../admin/order_list.html?statusId=2">已付款</a></li>
		<li role="presentation"><a href="../admin/order_list.html?statusId=3">配送中</a></li>
		<li role="presentation"><a href="../admin/order_list.html?statusId=4">已完成</a></li>
	</ul>

	<br>

	<table class="table table-bordered table-hover">

	<tr  id="dghead">
		<th width="5%">ID</th>
		<th width="5%">总价</th>
		<th width="15%">商品详情</th>
		<th width="20%">收货信息</th>
		<th width="10%">订单状态</th>
		<th width="10%">支付方式</th>
		<th width="10%">下单用户</th>
		<th width="10%">下单时间</th>
		<th width="10%" nowrap>操作</th>
	</tr>
	<tbody id="dgcontent">
	</tbody>
</table>
<br>
<div style='text-align:center;'>
    <a class='btn btn-info' id="first" href="javascript:first()">首页</a>
    <a class='btn btn-info' id="prev" href="javascript:prev()">上一页</a>
    <h3 style='display:inline;'>[<span id="currentPage"></span>/<span id="totalPage"></span>]</h3>
    <h3 style='display:inline;'>[<span id="recordsCount"></span>]</h3>
    <a class='btn btn-info' id="next" href="javascript:next()">下一页</a>
    <a class='btn btn-info' id="last" href="javascript:last()">尾页</a>
    <input type='text' class='form-control' id="current" style='display:inline;width:60px;'/><a class='btn btn-info' href='javascript:void(0);' onclick='jump()'>GO</a>
</div>
<br>
</div>
</body>
<script type="text/javascript">
var translateUsers = [];
var translateGoodses = [];
var mypage = 1;
var myrows = 10;
var mystatus = 0;
var currentPage = 1;
var pageCount = 1;
var status = "0";
$(document).ready(function(){
  checkAdminAccess();
  setTimeout(function() {
    translateUsers = translateListActiveUsers();
    translateGoodses = translateListActiveGoodses();
    var params = new URLSearchParams(window.location.search);
    var statusStr = params.get("statusId");
    if (isBlank(statusStr)) status = "0";
    else status = statusStr;
    loadDataGrid(mypage,myrows);
  }, 1000);
});

function loadDataGrid(page,rows){
	$.ajax({
		type:"post",
		url: "../../custcontroller/adminCustOrderCustController/order_list",
		data: JSON.stringify({
		    page : page,
			rows : rows,
			statusId: parseInt(status),
		}),
		dataType: 'json',
		contentType:"application/json;charset=UTF-8",
		success: function(data, textStatus) {
			debugger;
			if ( data.rows !=null ){
                $("#dgcontent").html("");
                configPageControls(data.totalPage,data.pageNumber,data.totalCount);
                for (let i=0;i<data.rows.length;i++){
    				appendGridRow(data.rows[i]);
                }
   			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function appendGridRow(row){
  var rowStr = "<tr id='girdrow'>\n"+
                "<td><p>"+row.id+"</p></td>\n"+
                "<td><p>"+row.total+"</p></td>\n"+
                "<td>"+genOrderItemList(row.id)+"</td>\n"+
                "<td><p>"+row.name+"</p>\n"+
                "<p>"+row.phone+"</p>\n"+
                "<p>"+row.address+"</p>\n"+
                "<td><p>"+lookupCustOrderStatus(row.statusId)+"</p></td>\n"+
                "<td><p>"+lookupPayType(row.payTypeId)+"</p></td>\n"+
             	"<td><p>"+translateUser(row.userId)+"</p></td>\n"+
				"<td><p>"+row.orderDateTime+"</p></td>\n"+
                "</td>\n"+
				genOperationTd(row) +
                "</tr>\n";
  $("#dgcontent").append(rowStr);
}

function showCoverImage(row){
	if(row.cover){
		return "<img style='height:100px;' border='1' src='data:image/png;base64,"+row.cover+"'/>";
	} else {
		return "<img style='height:100px;' border='1' src='../../images/blank.jpg'/>";
	}
}

function genOperationTd(row){
	var tdStr =	"<td nowrap>\n";
	if (row.statusId ==1) {
	   tdStr += "<a class=\"btn btn-success\" href=\"javascript:updateCustOrderStatus("+row.id+",2)\">付款</a>\n";
	} else if (row.statusId ==2) {
	   tdStr += "<a class=\"btn btn-success\" href=\"javascript:updateCustOrderStatus("+row.id+",3)\">发货</a>\n";
	} else if (row.statusId == 3) {
	   tdStr += "<a class=\"btn btn-warning\" href=\"javascript:updateCustOrderStatus("+row.id+",4)\">完成</a>\n";
	}
	tdStr += "<a class=\"btn btn-danger\" href=\"javascript:softDeleteCustOrder("+row.id+")\">软删除</a>\n";
	tdStr += "<a class=\"btn btn-danger\" href=\"javascript:deleteCustOrder("+row.id+")\">删除</a>\n";
	tdStr += "</td>\n";
	return tdStr;
}

function lookupCustOrderStatus(statusId){
  if (statusId ==1) return "未付款";
  if (statusId ==2) return "已付款";
  if (statusId ==3) return "已发货";
  if (statusId ==4) return "已完成";
}

function lookupPayType(statusId){
  if (statusId ==1) return "微信";
  if (statusId ==2) return "支付宝";
  if (statusId ==3) return "货到付款";
}

function genOrderItemList(custOrderId){
  var orderItems = listMyOrderItems(custOrderId);
  var tdStr =	"<p>\n";
  for (var i=0;i<orderItems.length;i++){
    tdStr += "<p>"+translateGoods(orderItems[i].goodsId)+" ("+orderItems[i].price+") x "+orderItems[i].amount+"</p>\n";
  }
  tdStr += "</p>\n";
  return tdStr;
}

function listMyOrderItems(custOrderId){
    var orderItems = [];
    $.ajax({
		type: "post",
		url: "../../custcontroller/adminCustOrderCustController/listMyOrderItems",
		data: JSON.stringify({
			"custOrderId":custOrderId,
		}),
		dataType: 'json',
		contentType:"application/json;charset=UTF-8",
		async:false,
		success: function(data, textStatus) {
     		if (data.success == true){
    		   orderItems = data.rows;
     		}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
    return orderItems;
}

function softDeleteCustOrder(id){
	$.ajax({
		type: "post",
		url: "../../controller/custOrderController/softDeleteCustOrder",
		data: {
			"id":id
		},
		dataType: 'json',
		success: function(data, textStatus) {
    		if (data.success == true){
    		    window.location.reload();
    		}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function deleteCustOrder(id){
	$.ajax({
		type: "post",
		url: "../../custcontroller/adminCustOrderCustController/deleteCustOrderWithOrderItems",
		dataType: 'json',
		data:{"id":id},
		success: function(data, textStatus) {
		    if (data.success == true){
    		    window.location.reload();
    		}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}


function updateCustOrderStatus(id,statusId){
	$.ajax({
		type: "post",
		url: "../../custcontroller/adminCustOrderCustController/updateCustOrderStatus",
		data: JSON.stringify({
			"id":id,
			"statusId":statusId,
		}),
		dataType: 'json',
		contentType:"application/json;charset=UTF-8",
		success: function(data, textStatus) {
    		if (data.success == true){
    		    window.location.reload();
    		}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function translateUser(value){
	if (isBlank(value) || parseInt(value) <= 0) return "";
	var retVal = value;
	if (translateUsers != null && translateUsers != undefined && translateUsers.length > 0){
		for(var i=0;i<translateUsers.length;i++){
			if (translateUsers[i].id == value) return translateUsers[i].userName;
		}
	}
	return retVal;
}

function translateListActiveUsers(value){
	var retVal = [];
	$.ajax({
		type:"post",
		url: "../../controller/userController/listActiveUsers",
		data: {
			id:value
		},
		dataType: 'json',
		async:false,
		success: function(data, textStatus) {
			debugger;
			if ( data.rows !=null ){
				retVal = data.rows;
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
	return retVal;
}

function translateGoods(value){
	if (isBlank(value) || parseInt(value) <= 0) return "";
	var retVal = value;
	if (translateGoodses != null && translateGoodses != undefined && translateGoodses.length > 0){
		for(var i=0;i<translateGoodses.length;i++){
			if (translateGoodses[i].id == value) return translateGoodses[i].name;
		}
	}
	return retVal;
}

function translateListActiveGoodses(value){
	var retVal = [];
	$.ajax({
		type:"post",
		url: "../../controller/goodsController/listActiveGoodses",
		data: {
			id:value
		},
		dataType: 'json',
		async:false,
		success: function(data, textStatus) {
			debugger;
			if ( data.rows !=null ){
				retVal = data.rows;
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
	return retVal;
}
</script>
</html>
